<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Using function hooks</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../intrusive.html" title="Chapter 19. Boost.Intrusive">
<link rel="prev" href="clone_from.html" title="Cloning Boost.Intrusive containers">
<link rel="next" href="recursive.html" title="Recursive Boost.Intrusive containers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="clone_from.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="recursive.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="intrusive.function_hooks"></a><a class="link" href="function_hooks.html" title="Using function hooks">Using function hooks</a>
</h2></div></div></div>
<p>
      A programmer might find that base or member hooks are not flexible enough in
      some situations. In some applications it would be optimal to put a hook deep
      inside a member of a class or just outside the class. <span class="bold"><strong>Boost.Intrusive</strong></span>
      has an easy option to allow such cases: <code class="computeroutput"><a class="link" href="../boost/intrusive/function_hook.html" title="Struct template function_hook">function_hook</a></code>.
    </p>
<p>
      This option is similar to <code class="computeroutput"><a class="link" href="../boost/intrusive/member_hook.html" title="Struct template member_hook">member_hook</a></code>
      or <code class="computeroutput"><a class="link" href="../boost/intrusive/base_hook.html" title="Struct template base_hook">base_hook</a></code>, but
      the programmer can specify a function object that tells the container how to
      obtain a hook from a value and vice versa. The programmer just needs to define
      the following function object:
    </p>
<pre class="programlisting"><span class="comment">//This functor converts between value_type and a hook_type</span>
<span class="keyword">struct</span> <span class="identifier">Functor</span>
<span class="special">{</span>
   <span class="comment">//Required types</span>
   <span class="keyword">typedef</span> <span class="comment">/*impl-defined*/</span>      <span class="identifier">hook_type</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="comment">/*impl-defined*/</span>      <span class="identifier">hook_ptr</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="comment">/*impl-defined*/</span>      <span class="identifier">const_hook_ptr</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="comment">/*impl-defined*/</span>      <span class="identifier">value_type</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="comment">/*impl-defined*/</span>      <span class="identifier">pointer</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="comment">/*impl-defined*/</span>      <span class="identifier">const_pointer</span><span class="special">;</span>
   <span class="comment">//Required static functions</span>
   <span class="keyword">static</span> <span class="identifier">hook_ptr</span> <span class="identifier">to_hook_ptr</span> <span class="special">(</span><span class="identifier">value_type</span> <span class="special">&amp;</span><span class="identifier">value</span><span class="special">);</span>
   <span class="keyword">static</span> <span class="identifier">const_hook_ptr</span> <span class="identifier">to_hook_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">value_type</span> <span class="special">&amp;</span><span class="identifier">value</span><span class="special">);</span>
   <span class="keyword">static</span> <span class="identifier">pointer</span> <span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">hook_ptr</span> <span class="identifier">n</span><span class="special">);</span>
   <span class="keyword">static</span> <span class="identifier">const_pointer</span> <span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">const_hook_ptr</span> <span class="identifier">n</span><span class="special">);</span>
<span class="special">};</span>
</pre>
<p>
      Converting from values to hooks is generally easy, since most hooks are in
      practice members or base classes of class data members. The inverse operation
      is a bit more complicated, but <span class="bold"><strong>Boost.Intrusive</strong></span>
      offers a bit of help with the function <code class="computeroutput"><a class="link" href="../boost/intrusive/get_parent_from_m_idm34802.html" title="Function template get_parent_from_member">get_parent_from_member</a></code>,
      which allows easy conversions from the address of a data member to the address
      of the parent holding that member. Let's see a little example of <code class="computeroutput"><a class="link" href="../boost/intrusive/function_hook.html" title="Struct template function_hook">function_hook</a></code>:
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">intrusive</span><span class="special">/</span><span class="identifier">list</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">intrusive</span><span class="special">/</span><span class="identifier">parent_from_member</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive</span><span class="special">;</span>

<span class="keyword">struct</span> <span class="identifier">MyClass</span>
<span class="special">{</span>
   <span class="keyword">int</span> <span class="identifier">dummy</span><span class="special">;</span>
   <span class="comment">//This internal type has a member hook</span>
   <span class="keyword">struct</span> <span class="identifier">InnerNode</span>
   <span class="special">{</span>
      <span class="keyword">int</span> <span class="identifier">dummy</span><span class="special">;</span>
      <span class="identifier">list_member_hook</span><span class="special">&lt;&gt;</span> <span class="identifier">hook</span><span class="special">;</span>
   <span class="special">}</span> <span class="identifier">inner</span><span class="special">;</span>
<span class="special">};</span>

<span class="comment">//This functor converts between MyClass and InnerNode's member hook</span>
<span class="keyword">struct</span> <span class="identifier">Functor</span>
<span class="special">{</span>
   <span class="comment">//Required types</span>
   <span class="keyword">typedef</span> <span class="identifier">list_member_hook</span><span class="special">&lt;&gt;</span>    <span class="identifier">hook_type</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="identifier">hook_type</span><span class="special">*</span>            <span class="identifier">hook_ptr</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">hook_type</span><span class="special">*</span>      <span class="identifier">const_hook_ptr</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="identifier">MyClass</span>               <span class="identifier">value_type</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="identifier">value_type</span><span class="special">*</span>           <span class="identifier">pointer</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">*</span>     <span class="identifier">const_pointer</span><span class="special">;</span>

   <span class="comment">//Required static functions</span>
   <span class="keyword">static</span> <span class="identifier">hook_ptr</span> <span class="identifier">to_hook_ptr</span> <span class="special">(</span><span class="identifier">value_type</span> <span class="special">&amp;</span><span class="identifier">value</span><span class="special">)</span>
      <span class="special">{</span>  <span class="keyword">return</span> <span class="special">&amp;</span><span class="identifier">value</span><span class="special">.</span><span class="identifier">inner</span><span class="special">.</span><span class="identifier">hook</span><span class="special">;</span> <span class="special">}</span>
   <span class="keyword">static</span> <span class="identifier">const_hook_ptr</span> <span class="identifier">to_hook_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">value_type</span> <span class="special">&amp;</span><span class="identifier">value</span><span class="special">)</span>
      <span class="special">{</span>  <span class="keyword">return</span> <span class="special">&amp;</span><span class="identifier">value</span><span class="special">.</span><span class="identifier">inner</span><span class="special">.</span><span class="identifier">hook</span><span class="special">;</span> <span class="special">}</span>
   <span class="keyword">static</span> <span class="identifier">pointer</span> <span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">hook_ptr</span> <span class="identifier">n</span><span class="special">)</span>
   <span class="special">{</span>
      <span class="keyword">return</span> <span class="identifier">get_parent_from_member</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;</span>
         <span class="special">(</span><span class="identifier">get_parent_from_member</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">InnerNode</span><span class="special">&gt;(</span><span class="identifier">n</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">InnerNode</span><span class="special">::</span><span class="identifier">hook</span><span class="special">)</span>
         <span class="special">,&amp;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">inner</span>
      <span class="special">);</span>
   <span class="special">}</span>
   <span class="keyword">static</span> <span class="identifier">const_pointer</span> <span class="identifier">to_value_ptr</span><span class="special">(</span><span class="identifier">const_hook_ptr</span> <span class="identifier">n</span><span class="special">)</span>
   <span class="special">{</span>
      <span class="keyword">return</span> <span class="identifier">get_parent_from_member</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">&gt;</span>
         <span class="special">(</span><span class="identifier">get_parent_from_member</span><span class="special">&lt;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">InnerNode</span><span class="special">&gt;(</span><span class="identifier">n</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">InnerNode</span><span class="special">::</span><span class="identifier">hook</span><span class="special">)</span>
         <span class="special">,&amp;</span><span class="identifier">MyClass</span><span class="special">::</span><span class="identifier">inner</span>
      <span class="special">);</span>
   <span class="special">}</span>
<span class="special">};</span>

<span class="comment">//Define a list that will use the hook accessed through the function object</span>
<span class="keyword">typedef</span> <span class="identifier">list</span><span class="special">&lt;</span> <span class="identifier">MyClass</span><span class="special">,</span> <span class="identifier">function_hook</span><span class="special">&lt;</span> <span class="identifier">Functor</span><span class="special">&gt;</span> <span class="special">&gt;</span>  <span class="identifier">List</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="identifier">MyClass</span> <span class="identifier">n</span><span class="special">;</span>
   <span class="identifier">List</span> <span class="identifier">l</span><span class="special">;</span>
   <span class="comment">//Insert the node in both lists</span>
   <span class="identifier">l</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">n</span><span class="special">);</span>
   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2005 Olaf Krzikalla<br>Copyright © 2006-2015 Ion Gaztanaga<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="clone_from.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intrusive.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="recursive.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
